package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.scity.beens.XgmmBeen;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class XgmmActivity extends AppCompatActivity {
    EditText editTextoldpwd;
    EditText editTextnewpwd;
    EditText editTextqueding;
    Button buttonpwd;
    XgmmBeen xgmmBeen;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(XgmmActivity.this, xgmmBeen.msg, Toast.LENGTH_LONG).show();
                    if (xgmmBeen.code == 200) {
                        Configs.Token = "";
                        finish();
                        startActivity(new Intent(XgmmActivity.this, LoginActivity.class));
                    }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xgmm);
        editTextnewpwd= findViewById(R.id.xgmm_xinmima);
        editTextoldpwd = findViewById(R.id.xgmm_yuanmima);
        editTextqueding = findViewById(R.id.xgmm_quedingmima);
        buttonpwd=findViewById(R.id.xgmm_btn);
        setPassword();
    }
    private void setPassword() {
        buttonpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Configs.Token.equals("")) {
                    startActivity(new Intent(XgmmActivity.this, LoginActivity.class));
                } else {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("oldPassword", editTextoldpwd.getText().toString());
                    map.put("newPassword", editTextnewpwd.getText().toString());

                    OkHttpClient okHttpClient = new OkHttpClient();
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType, new Gson().toJson(map));
                    Request request = new Request.Builder()
                            .url(Configs.Host + APIInterface.SetPwd)
                            .addHeader("Authorization", Configs.Token)
                            .put(requestBody)
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        }
                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            xgmmBeen = new Gson().fromJson(response.body().string(), XgmmBeen.class);
                            handler.sendEmptyMessage(0);
                        }
                    });
                }
            }
        });
    }
}

